//
//  TableViewItem.swift
//  Lease_IOS
//
//  Created by Mr.wang on 2018/12/20.
//  Copyright © 2018 Mr.wang. All rights reserved.
//

import UIKit
import RxDataSources

struct TableViewSection {
    var header: String
    var footer: String
    var items: [TableViewItem]
    
    static func section(header: String = "",
                        footer: String = "",
                        items: [TableViewItem]) -> TableViewSection {
        return TableViewSection(header: header, footer: footer, items: items)
    }
}

extension TableViewSection: SectionModelType {
    typealias Item = TableViewItem
    
    init(original: TableViewSection, items: [TableViewItem]) {
        self = original
        self.items = items
    }
}

typealias ItemSetUp = (TableViewItem) -> Void
typealias CellSelect = () -> Void
typealias TextFieldSetUp = (UITextField) -> ()

class TableViewItem {

    enum AccessoryType {
        case none
        case indicator
    }
    
    enum SepatorType {
        case normal
        case none
    }
    
    var title: String = ""
    
    var cellHeight: CGFloat = 50
    var detailTitle: String = ""
    var accessoryType: AccessoryType = .indicator
    
    var selectedStyle: UITableViewCell.SelectionStyle = .default
    var sepatorType: SepatorType = .normal
    
    var cellSelect: CellSelect = { }
    var textFieldSetUp: TextFieldSetUp? = nil
    var textFieldValue: String = ""
    
    init(title: String = "") {
        self.title = title
    }
    
    @discardableResult
    func cellSelect(_ closure: @escaping CellSelect) -> TableViewItem {
        self.cellSelect = closure
        return self
    }
    
    @discardableResult
    func itemSetUp(_ closure: @escaping ItemSetUp) -> TableViewItem {
        closure(self)
        return self
    }
    
    func textFieldSetUpUpdate(_ closure: @escaping TextFieldSetUp) -> TableViewItem {
        self.textFieldSetUp = closure
        self.selectedStyle = .none
        return self
    }
    
    func handleClick() {
        cellSelect()
    }
    
}
